<?php

namespace App\Models;

use App\Models\dispatch\PlaceModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class OrderModel extends BaseModel
{
    use HasFactory;

    protected $table = 't_order';

    protected $primaryKey = 'id';

    public $timestamps = true;

    const CREATED_AT = 'created_at';
    
    const UPDATED_AT = null;

    protected $guarded = [];

    protected $hidden = [
        'deleted_at',
    ];

    //状态 1预报下单 2确认下单 3已入库 4已出库 5已抵达 6已清关 7已签收 8退件
    const STATUS_PREORDER = 1;
    const STATUS_CONFIRM = 2;
    const STATUS_STORAGE = 3;
    const STATUS_OUTSTOCK = 4;
    const STATUS_ARRIVE = 5;
    const STATUS_CLEARANCE = 6;
    const STATUS_SIGNED = 7;
    const STATUS_RETURN = 8;
    const STATUS_TAKEOFF = 9; //起飞
    const STATUS_DECLARE = 10; //报关
    const STATUS_CHECK = 11; //查验
    const STATUS_PASS = 12; //放行

    const STATUS_MAPS = [
        self::STATUS_PREORDER => '预报下单',
        self::STATUS_CONFIRM => '确认下单',
        self::STATUS_STORAGE => '已入库',
        self::STATUS_OUTSTOCK => '已出库',
        self::STATUS_DECLARE => '报关',
        self::STATUS_CHECK => '查验',
        self::STATUS_PASS => '放行',
        self::STATUS_ARRIVE => '已抵达',
        self::STATUS_CLEARANCE => '已清关',
        self::STATUS_SIGNED => '已派送',
        self::STATUS_RETURN => '退件',
        self::STATUS_TAKEOFF => '已起飞',

    ];

    const CUSTOMER_STATUS_MAPS = [
        self::STATUS_PREORDER => '预报下单',
        self::STATUS_CONFIRM => '确认下单',
        self::STATUS_STORAGE => '已入库',
        self::STATUS_ARRIVE => '已抵达',
        self::STATUS_TAKEOFF => '已起飞',
    ];

    //货物属性 1普货 2特货
    const GOODS_ATTR_GENERAL = 1;
    const GOODS_ATTR_SPECIAL = 2;
    const GOODS_ATTR_MAPS = [
        self::GOODS_ATTR_GENERAL => '普货',
        self::GOODS_ATTR_SPECIAL => '敏货',
    ];

    //报关方式 1非退税 2退税（一般贸易） 3退税（医疗器械）
    const CLEARANCE_MODE_NO = 1;
    const CLEARANCE_MODE_GENERAL = 2;
    const CLEARANCE_MODE_MEDICAL = 3;
    const CLEARANCE_MODE_MAPS = [
        self::CLEARANCE_MODE_NO => '非退税',
        self::CLEARANCE_MODE_GENERAL => '退税（一般贸易）',
        self::CLEARANCE_MODE_MEDICAL => '退税（医疗器械）',
    ];

    //包装 1麻袋 2木架 3纸箱 4木托 5卡板 6木箱
    const PACKAGE_BAG = 1;
    const PACKAGE_WOOD = 2;
    const PACKAGE_BOX = 3;
    const PACKAGE_PALLET = 4;
    const PACKAGE_CARD = 5;
    const PACKAGE_WOOD_BOX = 6;
    const PACKAGE_MAPS = [
        self::PACKAGE_BAG => '麻袋',
        self::PACKAGE_WOOD => '木架',
        self::PACKAGE_BOX => '纸箱',
        self::PACKAGE_PALLET => '木托',
        self::PACKAGE_CARD => '卡板',
        self::PACKAGE_WOOD_BOX => '木箱',
    ];
    

    //配舱状态 1未配舱 2配舱未填写提单号 3已填写提单号
    const CARGO_STATUS_NO = 1;
    const CARGO_STATUS_NO_FILL = 2;
    const CARGO_STATUS_FILL = 3;
    const CARGO_STATUS_CONFIRM = 4;
    const CARGO_STATUS_MAPS = [
        self::CARGO_STATUS_NO => '未配舱',
        self::CARGO_STATUS_NO_FILL => '未填写提单号',
        self::CARGO_STATUS_FILL => '已填写提单号',
        self::CARGO_STATUS_CONFIRM => '确认配舱'
    ];


    const HK_ENTER_MODE = [20, 21];

    //单证报关方式
    public static function separateDecMethod($method){
        //1一般贸易 2医疗器械 3非退税s
        $map = [1=>'一般贸易',2=>'医疗器械',3=>'非退税'];
        return $map[$method] ?? '';
    }


    //创建人
    public function create_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'created_admin_id');
    }

    //提单地址
    public function take_order_addr(): HasOne {
        return $this->hasOne(OrderReceiveModel::class, 'order_id')
            ->where('type', OrderReceiveModel::TAKE_TYPE);
    }

    //分单地址
    public function split_order_addr(): HasMany {
        return $this->hasMany(OrderReceiveModel::class, 'order_id')
            ->where('type', OrderReceiveModel::SPLIT_TYPE);
    }

    //客户
    public function customer(): BelongsTo {
        return $this->belongsTo(CustomerModel::class, 'customer_id');
    }

    //提单
    public function take_order(): BelongsTo {
        return $this->belongsTo(TakeOrderModel::class, 'take_order_id');
    }

    //目的地国家
    public function des_country(): BelongsTo {
        return $this->belongsTo(CountryModel::class, 'des_country_id');
    }

    //目的地国家
    public function des_city(): BelongsTo {
        return $this->belongsTo(CountryModel::class, 'des_city_id');
    }

    public function airport(): BelongsTo {
        return $this->belongsTo(AirportModel::class, 'airport_id');
    }

    //入库信息
    public function volumes(): HasMany {
        return $this->hasMany(OrderVolumeModel::class, 'order_id');
    }

    //航班运力
    public function flight(): BelongsTo {
        return $this->belongsTo(FlightModel::class, 'flight_id');
    }

    //库位
    public function place(): BelongsTo {
        return $this->belongsTo(PlaceModel::class, 'place_id');
    }

}
